package com.objecteffects.ilmp.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.scribe.model.Token;

import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * @author Rusty Wright
 */
@Entity
public class TumblrBlogEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;

    @ElementCollection(fetch = FetchType.EAGER)
    private Set<String> urls = new HashSet<>();

    @Column(nullable = false)
    private Token accessToken;

    @OneToMany(orphanRemoval = true, cascade = {
        CascadeType.ALL
    }, fetch = FetchType.EAGER)
    private List<BlogPostsOffsetEntity> blogPostsOffsetEntity =
        new ArrayList<>();

    @Column(nullable = false)
    private String debugSecret;

    @Column(nullable = false)
    private String debugToken;

    public TumblrBlogEntity() {
    }

    public TumblrBlogEntity(final Set<String> _urls, final Token _accessToken) {
        this.urls = _urls;
        this.accessToken = _accessToken;
        this.debugSecret = _accessToken.getSecret();
        this.debugToken = _accessToken.getToken();
    }

    public TumblrBlogEntity(final String _url, final Token _accessToken) {
        this.urls.add(_url);
        this.accessToken = _accessToken;
        this.debugSecret = _accessToken.getSecret();
        this.debugToken = _accessToken.getToken();
    }

    /**
     * @return the Id
     */
    public final Long getId() {
        return this.id;
    }

    /**
     * @param _id
     *        the id to set
     */
    public final void setId(final Long _id) {
        this.id = _id;
    }

    /**
     * @return the url
     */
    public final Set<String> getUrls() {
        return this.urls;
    }

    /**
     * @param _urls
     *        the urls to set
     */
    public final void setUrls(final Set<String> _urls) {
        this.urls = _urls;
    }

    /**
     * @return the accessToken
     */
    public final Token getAccessToken() {
        return this.accessToken;
    }

    /**
     * @param _accessToken
     *        the accessToken to set
     */
    public final void setAccessToken(final Token _accessToken) {
        this.accessToken = _accessToken;
        this.debugSecret = _accessToken.getSecret();
        this.debugToken = _accessToken.getToken();
    }

    public final String getDebugSecret() {
        return this.debugSecret;
    }

    public final String getDebugToken() {
        return this.debugToken;
    }

    /**
     * @return the blogPostsOffsetEntity
     */
    public final List<BlogPostsOffsetEntity> getBlogPostsOffsetEntity() {
        return this.blogPostsOffsetEntity;
    }

    /**
     * @param _blogPostsOffsetEntity
     *        the blogPostsOffsetEntity to set
     */
    public final void setBlogPostsOffsetEntity(
        final List<BlogPostsOffsetEntity> _blogPostsOffsetEntity) {
        this.blogPostsOffsetEntity = _blogPostsOffsetEntity;
    }

    @Override
    public final String toString() {
        return new ToStringBuilder(this).append("id", this.id)
            .append("urls", this.urls).append("accessToken", this.accessToken)
            .append("blogPostsOffsetEntity", this.blogPostsOffsetEntity)
            .toString();
    }
}
